package com.xw.bag;

import java.sql.SQLException;
import java.util.ArrayList;


import com.wzsck.util.DBManager;
import com.wzsck.util.QueryDataResult;

public class XWManager {
	//查询query
			public ArrayList<xw> query(){//把东西存到动态数组里面
				ArrayList<xw> list = new ArrayList<xw>();
				String sql = "select * from wzs_journalism";//sql语句查表book里的东西
				QueryDataResult qdr =DBManager.executeQuery(sql);
				if (qdr.rs!=null){
					try {
						while (qdr.rs.next()){
							int id=qdr.rs.getInt("id");//编号,后面是获取book实体类里的id getInt（“id”）
							String name=qdr.rs.getString("name");//书名
							String imgurl=qdr.rs.getString("imgurl");//图片
							String neirong=qdr.rs.getString("neirong");
							String time=qdr.rs.getString("time");//价格
							xw x= new xw(id,name,imgurl,neirong,time);
							list.add(x);
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				return list;
			}
			//add
			public int add( String name,String imgurl, String neirong,String time) {
				String sql = String.format(
						"insert into wzs_journalism(`name`,`imgurl`,`neirong`,`time`) values('%s','%s','%s','%s')",name,imgurl,neirong,time);
				
				System.out.print(sql);
				int i = DBManager.executeUpdate(sql);
				return i;

			}
			
		
			public int update(String id,  String name,String imgurl, String neirong,String time) {
				System.out.print("更新");
				String sql = String.format(
						"  update wzs_journalism set `name`='%s',`imgurl`='%s',`neirong`='%s' ,`time` ='%s' where id=%s  ",
						 name,imgurl,neirong,time,id);
				System.out.print(sql);
				
				int i = DBManager.executeUpdate(sql);
				return i;
			}
			public int del(int id) {
				String sql = "delete from wzs_journalism where id =  " + id;
				System.out.println(sql);
				int i = DBManager.executeUpdate(sql);
				return i;
			}
			
			//==========================================================
			 public int count() {//count查非空
			 		String sql="select count(*) from wzs_journalism";//换表名
			 		QueryDataResult qdr=DBManager.executeQuery(sql);
			 		if (qdr.rs!=null) {
			 			try {
			 				while (qdr.rs.next()) {
			 					int count =qdr.rs.getInt("count(*)");
			 					return count;
			 				}
			 			} catch (SQLException e) {
			 				// TODO Auto-generated catch block
			 				e.printStackTrace();
			 			}
			 		}
			 		return 0;
			 	}
			 
			 
			   public ArrayList<xw> chaxun(int page,int limit) {
			 	    ArrayList<xw> list =new ArrayList<xw>();//limit返回行数 limit 开始数 , 行数
						String sql="select * from  wzs_journalism  limit "+(page - 1)*limit+","+limit;//换表明
						QueryDataResult qdr=DBManager.executeQuery(sql);
						xw x=null;
						if (qdr.rs!=null){
							try {
								while (qdr.rs.next()){
									int id=qdr.rs.getInt("id");//编号,后面是获取book实体类里的id getInt（“id”）
									String name=qdr.rs.getString("name");//书名
									String imgurl=qdr.rs.getString("imgurl");//图片
									String neirong=qdr.rs.getString("neirong");
									String time=qdr.rs.getString("time");//价格
									x= new xw(id,name,imgurl,neirong,time);
									list.add(x);
								}
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						qdr.close();
						return list;		
				} 
}
